package inet.app.tuvi.activity;

import inet.app.tuvi.R;
import inet.app.tuvi.models.DeviceInfoModel;
import inet.app.tuvi.util.BitmapLargeManager;
import inet.app.tuvi.util.BitmapLargeManager.LoadBitmapListener;
import inet.app.tuvi.util.DeviceUtil;
import inet.app.tuvi.util.LaunchIntent;
import inet.app.tuvi.util.SmartLog;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

public class SplashActivity extends Activity {
	
	private final static int TIME_WAIT = 2000;
	private View mViewLoading;
	private ImageView mSplashImg;
	
	CountDownTimer mTimer = new CountDownTimer(TIME_WAIT, TIME_WAIT) {

		@Override
		public void onTick(long millisUntilFinished) {
		}

		@Override
		public void onFinish() {
			LaunchIntent.start(SplashActivity.this, MainActivity.class);
			finish();
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//full screen
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		
		setContentView(R.layout.activity_splash);
		initModels();
		initViews();
	}
	
	private void initModels() {
		
	}
	
	private void initViews() {
		mViewLoading = findViewById(R.id.loading_layout);
		mViewLoading.setVisibility(View.GONE);
		mSplashImg = (ImageView) findViewById(R.id.splash_image);
		loadSplashImage();
	}
	
	private void loadSplashImage() {
		DeviceInfoModel device = DeviceUtil.getDeviceInfo(this);
		int widthDevice = device.width;
		int heightDevice = device.height;
		final Context appContext = getApplicationContext();
		
		mViewLoading.setVisibility(View.VISIBLE);
		BitmapLargeManager.getInstance(appContext).loadLargeBimap(
				R.drawable.manhinhcho, widthDevice, heightDevice, new LoadBitmapListener() {
					@Override
					public void onLoadFinish(int resId, boolean isSuccess) {
						mViewLoading.setVisibility(View.GONE);
						if (isSuccess) {
							mSplashImg.setImageBitmap(BitmapLargeManager
									.getInstance(appContext)
									.getBitmapFromCache(resId));
						} else {
							SmartLog.e("load splash image failed");
						}
						mTimer.start();
					}
				});
	}
	
	@Override
	public void onPause() {
		super.onPause();
		mTimer.cancel();
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		
		//remove large bitmap
		BitmapLargeManager.getInstance(getApplicationContext()).removeBitmap(
				R.drawable.manhinhcho);
	}

}
